package com.panda.cn.utils.jwt;

import cn.hutool.json.JSONObject;
import com.panda.cn.component.SpringBeanUtil;
import com.panda.cn.entity.SysUserEntity;
import com.panda.cn.mapper.SysUserMapper;

public class WebContextUtil {
    private final static SysUserMapper sysUserMapper = SpringBeanUtil.getBean(SysUserMapper.class);
    
    //本地线程缓存token
    private static ThreadLocal<String> local = new ThreadLocal<>();
 
    /**
     * 设置token信息
     * @param content
     */
    public static void setUserToken(String content){
        removeUserToken();
        local.set(content);
    }
 
    /**
     * 获取token信息
     * @return
     */
    public static SysUserEntity getUserInfo(){
        if(local.get() != null){
            String userCode = JwtUtil.getUserId(local.get());
            JSONObject params = new JSONObject();
            params.set("code", userCode);
            return sysUserMapper.findLoginUser(params);
        }
        return null;
    }
 
    /**
     * 移除token信息
     * @return
     */
    public static void removeUserToken(){
        if(local.get() != null){
            local.remove();
        }
    }
}